package cc.geguo.client;

import cc.geguo.state.HasMoneyState;
import cc.geguo.state.NoMoneyState;
import cc.geguo.state.SoldOutState;
import cc.geguo.state.SoldState;
import cc.geguo.state.State;
import cc.geguo.state.WinnerState;

/**
 * 作者  Administrator
 * 时间  2017/4/24 17:53
 * 文件  StatePatter
 * 描述  需要保存 状态和条件
 *
 * 客户：依赖所有的状态
 *      只维护一种状态
 */
public class VendingMachine {
	private State noMoneyState;
	private State hasMoneyState;
	private State soldState;
	private State soldOutState;

	public State getNoMoneyState() {
		return noMoneyState;
	}

	public State getHasMoneyState() {
		return hasMoneyState;
	}

	public State getSoldState() {
		return soldState;
	}

	public State getSoldOutState() {
		return soldOutState;
	}

	public State getWinnerState() {
		return winnerState;
	}

	public State getCurrentState() {
		return currentState;
	}

	private State winnerState ;

	public int getCount() {
		return count;
	}

	private int count = 0;
	private State currentState = noMoneyState;

	public VendingMachine(int count) {
		noMoneyState = new NoMoneyState(this);
		hasMoneyState = new HasMoneyState(this);
		soldState = new SoldState(this);
		soldOutState = new SoldOutState(this);
		winnerState = new WinnerState(this);

		if (count > 0) {
			this.count = count;
			currentState = noMoneyState;
		}
	}

	public void insertMoney() {
		currentState.insertMoney();
	}

	public void backMoney() {
		currentState.backMoney();
	}

	public void turnCrank() {
		currentState.turnCrank();
		if (currentState == soldState || currentState == winnerState)
			currentState.dispense();
	}

	public void dispense() {
		System.out.println("发出一件商品...");
		if (count != 0) {
			count -= 1;
		}
	}

	public void setState(State state) {
		this.currentState = state;
	}

	//getter setter omitted ...
}
